/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cl.dfg.twittersneps;

import cl.dfg.twittersneps.modelo.Ngrams;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JTextPane;

/**
 *
 * @author Franco
 */
public class HashtableMaster {

    Ngrams tablaNgrams = new Ngrams();

    public void buscarNgrams(int tipo,String ngram){
        int contador = 1;

        if(tipo == 1){
            if(tablaNgrams.buscarUngram(ngram)){
               contador = tablaNgrams.getUngrams(ngram)+1;
            }
            //Almacena las estructuras dentro de la hashtable
            tablaNgrams.addUngrams(ngram, contador);
        }

        if(tipo == 2){
            if(tablaNgrams.buscarBigram(ngram)){
               contador = tablaNgrams.getBigrams(ngram)+1;
            }
            //Almacena las estructuras dentro de la hashtable
            tablaNgrams.addBigrams(ngram, contador);
        }

        if(tipo ==3){
            if(tablaNgrams.buscarTrigram(ngram)){
               contador = tablaNgrams.getTrigrams(ngram)+1;
            }
            //Almacena las estructuras dentro de la hashtable
            tablaNgrams.addTrigrams(ngram, contador);
        }

    }

    public Hashtable getHashtable(int tipo){
        if(tipo == 1)
            return tablaNgrams.getTablaUngram();
        if(tipo == 2)
            return tablaNgrams.getTablaBigram();
        if(tipo == 3)
            return tablaNgrams.getTablaTrigram();
        else
            return tablaNgrams.getTablaTrigram();
    }

    public void ordenarTabla(Hashtable tabla, JTextPane panel){

        panel.setText("");

        //Put keys and values in to an arraylist using entryset
        ArrayList myArrayList1=new ArrayList(tabla.entrySet());
        //Sort the values based on values first and then keys.
        Collections.sort(myArrayList1, new MyComparator());

          //Show sorted results
        Iterator itr1=myArrayList1.iterator();
        String key1="";
        int value1=0;
        int cnt1=0;
        while(itr1.hasNext()){
        cnt1++;
        Map.Entry e=(Map.Entry)itr1.next();
        key1 = (String)e.getKey();
        value1 = ((Integer)e.getValue()).intValue();
         if( value1 > 2 ){
                panel.setText(panel.getText()+"n-gram= "+key1+" ("+value1+")\n");

              }
        //System.out.println(key+" , "+value);
        }



    }


static class MyComparator implements Comparator{

public int compare(Object obj1, Object obj2){
int result=0;Map.Entry e1 = (Map.Entry)obj1 ;
Map.Entry e2 = (Map.Entry)obj2 ;//Sort based on values.
Integer value1 = (Integer)e1.getValue();
Integer value2 = (Integer)e2.getValue();
if(value1.compareTo(value2)==0){
String word1=(String)e1.getKey();
String word2=(String)e2.getKey();
//Sort String in an alphabetical order
result=word1.compareToIgnoreCase(word2);
} else{
//Sort values in a descending order
result=value2.compareTo( value1 );
}
return result;

}



}
}
